package com.qm.util;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import com.qm.model.Users;

public class UserExcelUpload {
	public ArrayList<Users> parseUser(MultipartFile excel) {
		XSSFWorkbook wb=null;
		try {
			wb=new XSSFWorkbook(excel.getInputStream());
		} catch (IOException e) {
			return null;
		}
		XSSFSheet sheet=wb.getSheetAt(0);
		XSSFRow headRow=sheet.getRow(0);
		HashMap<Integer,String> indexMap=new HashMap<Integer,String>();
		int colCount=headRow.getPhysicalNumberOfCells();
		for(int i=0;i<colCount;i++){
			XSSFCell cell=headRow.getCell(i);
			String cellVal=cell.getStringCellValue().toLowerCase();
			if(!cellVal.isEmpty()&&!indexMap.containsValue(cellVal)){
				indexMap.put(i,cellVal);
			}
		}
		int rowCount=sheet.getPhysicalNumberOfRows();
		ArrayList<Users> userList=new ArrayList<Users>();
		for(int i=1;i<rowCount;i++){
			XSSFRow row=sheet.getRow(i);
			ArrayList<Object> rowData=new ArrayList<Object>();
			Users user=new Users();
			//Method[]methods= user.getClass().getDeclaredMethods();
			for(int j=0;j<colCount;j++){
				XSSFCell cell=row.getCell(j);
				Method method=null;
				String methodName=indexMap.get(j);
				if(methodName==null||methodName.trim().isEmpty()){
					continue;
				}
				methodName=methodName.substring(0,1).toUpperCase()+methodName.substring(1);
				try {
					
				switch (cell.getCellType()) {
				case XSSFCell.CELL_TYPE_NUMERIC:
					//type=user.getClass().getDeclaredMethod("get"+methodName, null).getGenericReturnType();
					method=user.getClass().getDeclaredMethod("set"+methodName,Integer.class);
					method.invoke(user, (int)cell.getNumericCellValue());
					//rowData.add(cell.getNumericCellValue());
					break;
				case XSSFCell.CELL_TYPE_STRING:					
					method=user.getClass().getDeclaredMethod("set"+methodName,String.class);
					method.invoke(user, cell.getStringCellValue());
					break;
				case XSSFCell.CELL_TYPE_BOOLEAN:
					method=user.getClass().getDeclaredMethod("set"+methodName,Boolean.class);
					method.invoke(user, cell.getBooleanCellValue());
				default:
					break;
				}

				} catch (Exception e) {
					e.printStackTrace();
					//System.out.println(e.getLocalizedMessage());
					// TODO: handle exception
				}
			}
			System.out.println("ddd");
			userList.add(user);
		}
		return userList;
	}
	public Users getUserByRowData(ArrayList<Object>rowData,HashMap<String, Integer>index ){
		Users user=new Users();
		Method[]methods= user.getClass().getDeclaredMethods();
		for (Method method : methods) {
			if(method.getName().startsWith("set")){
				int i=index.get( method.getName().substring("set".length()).toLowerCase());
				try {
					Type type= method.getGenericParameterTypes()[0];
					
					method.invoke(user,rowData.get(i));
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return user;
	}
}
